/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "矩阵计算.h"



#include <A_CUDA数学Core/matnn.h>



Mat44 f_求逆矩阵(const Mat44& mat) {
	XMatrix<4, 4, float32> m2;
	memcpy(m2.data, mat.columns, sizeof(float32) * 16);

	float32 d;
	m2 = Inverse<4, float32>(m2, d);

	Mat44 im;
	memcpy(im.columns, m2.data, sizeof(float32) * 16);

	return im;
}

void f_求逆矩阵(C_Mat33& mat) {
	XMatrix<3, 3, float32> m2;
	memcpy(m2.data, mat.cols, sizeof(float32) * 9);

	float32 d;
	m2 = Inverse<3, float32>(m2, d);

	memcpy(mat.cols, m2.data, sizeof(float32) * 9);
}

mat3X3 f_mat_3x3转置(const mat3X3& m) {
	mat3X3 rt = {};

	rt.row0.x = m.row0.x;
	rt.row1.x = m.row0.y;
	rt.row2.x = m.row0.z;

	rt.row0.y = m.row1.x;
	rt.row1.y = m.row1.y;
	rt.row2.y = m.row1.z;

	rt.row0.z = m.row2.x;
	rt.row1.z = m.row2.y;
	rt.row2.z = m.row2.z;

	return rt;
}

void f_mat_转换(mat3X3& m, const C_Mat33& m2) {
	m.row0.x = m2.cols[0].x;
	m.row1.x = m2.cols[0].y;
	m.row2.x = m2.cols[0].z;

	m.row0.y = m2.cols[1].x;
	m.row1.y = m2.cols[1].y;
	m.row2.y = m2.cols[1].z;

	m.row0.z = m2.cols[2].x;
	m.row1.z = m2.cols[2].y;
	m.row2.z = m2.cols[2].z;
}

mat3X3 f_mat_转换(C_Mat44& m44) {
	mat3X3 m;
	m.row0.x = m44.columns[0][0];
	m.row1.x = m44.columns[0][1];
	m.row2.x = m44.columns[0][2];

	m.row0.y = m44.columns[1][0];
	m.row1.y = m44.columns[1][1];
	m.row2.y = m44.columns[1][2];

	m.row0.z = m44.columns[2][0];
	m.row1.z = m44.columns[2][1];
	m.row2.z = m44.columns[2][2];
	return m;
}



C_Mat33 f_mat_转换(const mat3X3& m2) {
	C_Mat33 m;
	m.cols[0].x = m2.row0.x;
	m.cols[0].y = m2.row0.y;
	m.cols[0].z = m2.row0.z;

	m.cols[1].x = m2.row1.x;
	m.cols[1].y = m2.row1.y;
	m.cols[1].z = m2.row1.z;

	m.cols[2].x = m2.row2.x;
	m.cols[2].y = m2.row2.y;
	m.cols[2].z = m2.row2.z;

	return m;
}

void f_mat_转换(C_Mat44& m, const C_Mat33& m2) {
	m = m.kIdentity;

	for (uint8 y = 0; y < 3; ++y) {
		for (uint8 x = 0; x < 3; ++x) {
			m.columns[y][x] = m2.cols[y][x];
		}
	}
	
}

void f_mat_转换(C_Mat44& m, const mat3X3& m2) {
	m = m.kIdentity;

	for (uint8 y = 0; y < 3; ++y) {
		for (uint8 x = 0; x < 3; ++x) {
			m.columns[y][x] = ((float32*)&m2)[y*3+x];
		}
	}
}
